﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSTDZ.ERP.Application.DataContracts;
using AutoMapper;
using XSTDZ.ERP.Domain.Models;
using XSTDZ.ERP.Core.Repositories;
using XSTDZ.ERP.Core.Generator;

namespace XSTDZ.ERP.Application.Implements
{
    public class RoleServiceImpl:ApplicationServiceBase,IRoleService
    {
        IRepository<Role> _roleRepository;
        public RoleServiceImpl(IRepositoryContext context, IRepository<Role> roleRepository)
            : base(context)
        {
            this._roleRepository = roleRepository;
        }

        public void AddRole(RoleDto roleDto)
        {
            var role = Mapper.Map<RoleDto, Role>(roleDto);
            role.Id = (Guid)new SequentialIdentityGenerator().Next;
            _roleRepository.Add(role);
            this.Context.Commit();
        }

        public void UpdateRole(RoleDto roleDto)
        {
            var role = Mapper.Map<RoleDto, Role>(roleDto);
            _roleRepository.Update(role);
            this.Context.Commit();
        }

        public void DeleteRole(Guid roleKey)
        {
            _roleRepository.Remove(r => r.Id == roleKey);
            this.Context.Commit();
        }

        public RoleDto GetRole(Guid roleKey)
        {
            var role = _roleRepository.Get(r => r.Id == roleKey);
            var roleDto = Mapper.Map<Role, RoleDto>(role);
            return roleDto;
        }

        public IEnumerable<RoleDto> GetAllRole()
        {
            var rolelst = _roleRepository.GetAll();
            return Mapper.Map<IEnumerable<Role>, IEnumerable<RoleDto>>(rolelst);
        }

     
    }
}
